<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>wddx examples</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="wddx.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="ref.wddx.html">WDDX 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="wddx.examples.html">范例</a></li>
    <li>wddx examples</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="wddx.examples-serialize" class="section">
  <h2 class="title">wddx examples</h2>
  <p class="para">
   All the functions that serialize variables use the first
   element of an array to determine whether the array is to be
   serialized into an array or structure. If the first element has
   string key, then it is serialized into a structure, otherwise,
   into an array.
  </p>
  
  <div class="example" id="example-5476">
   <p><strong>示例 #1 Serializing a single value with WDDX</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">wddx_serialize_value</span><span style="color: #007700">(</span><span style="color: #DD0000">"PHP&nbsp;to&nbsp;WDDX&nbsp;packet&nbsp;example"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PHP&nbsp;packet"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    This example will produce:
   </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
&lt;wddxPacket version=&#039;1.0&#039;&gt;&lt;header comment=&#039;PHP packet&#039;/&gt;&lt;data&gt;
&lt;string&gt;PHP to WDDX packet example&lt;/string&gt;&lt;/data&gt;&lt;/wddxPacket&gt;
</pre></div>
   </div>
  </div>

  <div class="example" id="example-5477">
   <p><strong>示例 #2 Using incremental packets with WDDX</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$pi&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3.1415926</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$packet_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">wddx_packet_start</span><span style="color: #007700">(</span><span style="color: #DD0000">"PHP"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">wddx_add_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$packet_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"pi"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Suppose&nbsp;$cities&nbsp;came&nbsp;from&nbsp;database&nbsp;*/<br /></span><span style="color: #0000BB">$cities&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"Austin"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Novato"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Seattle"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">wddx_add_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$packet_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"cities"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$packet&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">wddx_packet_end</span><span style="color: #007700">(</span><span style="color: #0000BB">$packet_id</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$packet</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    This example will produce:
   </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
&lt;wddxPacket version=&#039;1.0&#039;&gt;&lt;header comment=&#039;PHP&#039;/&gt;&lt;data&gt;&lt;struct&gt;
&lt;var name=&#039;pi&#039;&gt;&lt;number&gt;3.1415926&lt;/number&gt;&lt;/var&gt;&lt;var name=&#039;cities&#039;&gt;
&lt;array length=&#039;3&#039;&gt;&lt;string&gt;Austin&lt;/string&gt;&lt;string&gt;Novato&lt;/string&gt;
&lt;string&gt;Seattle&lt;/string&gt;&lt;/array&gt;&lt;/var&gt;&lt;/struct&gt;&lt;/data&gt;&lt;/wddxPacket&gt;
</pre></div>
   </div>
  </div>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Strings should be encoded in UTF-8; to handle other encodings, convert
    the string first using <span class="function"><a href="function.mb-convert-encoding.html" class="function">mb_convert_encoding()</a></span>,
    <span class="methodname"><a href="uconverter.transcode.html" class="methodname">UConverter::transcode()</a></span>, or <span class="function"><a href="function.iconv.html" class="function">iconv()</a></span>.
   </p>
  </p></blockquote>
 </div></div></div></body></html>